<template>
  <div>
    <h1>新增用户</h1>
    <el-form :model="userForm"
             :rules="rules"
             ref="userForm"
             label-width="100px"
             style="width: 600px"
             class="userForm">
      <el-form-item label="工号" prop="jobNumber">
        <el-input v-model="userForm.jobNumber"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="userForm.name"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="userForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="department">
        <el-input v-model="userForm.department"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="userForm.role" placeholder="请选择角色" style="width: 500px">
          <el-option label="管理员" value="管理员"></el-option>
          <el-option label="操作员" value="操作员"></el-option>
          <el-option label="工艺员" value="工艺员"></el-option>
          <el-option label="工艺巡检员" value="工艺巡检员"></el-option>
          <el-option label="设备管理员" value="设备管理员"></el-option>
          <el-option label="设备巡检员" value="设备巡检员"></el-option>
          <el-option label="设备维修员" value="设备维修员"></el-option>
          <el-option label="质检员" value="质检员"></el-option>
          <el-option label="中控管理员" value="中控管理员"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="userForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="userForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('userForm')">立即创建</el-button>
        <el-button @click="resetForm('userForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'AddUsers',
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.userForm.checkPass !== '') {
          this.$refs.userForm.validateField('checkPass');
        }
        callback();
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.userForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      userForm: {
        jobNumber: null,
        department: '',
        name: '',
        role: '',
        pass: '',
        checkPass: '',
      },
      rules: {
        jobNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' },
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
        ],
        department: [
          { required: true, message: '请选择部门', trigger: 'change' },
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'change' },
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' },
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      // eslint-disable-next-line consistent-return
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addUser();
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    addUser() {
      const { userForm, axios, open } = this;
      const postData = {};
      postData.jobNumber = userForm.jobNumber;
      postData.name = userForm.name;
      postData.phone = userForm.phone;
      postData.department = userForm.department;
      postData.role = userForm.role;
      postData.password = userForm.checkPass;
      axios({
        method: 'post',
        url: '/home/user/add_user/',
        timeout: 600000,
        data: postData,
      })
        .then(() => {
          open();
        })
        .catch((error) => {
          console.log(error.response.data);
          this.$message.error('添加失败');
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    open() {
      this.$alert('添加成功', '消息', {
        confirmButtonText: '确定',
        callback: () => {
          this.userForm = {};
        },
      });
    },
  },
};
</script>

<style scoped>

</style>
